package com.ypx;


import java.util.Comparator;
import java.util.TreeSet;

public class TreeSetDemo {
    public static void main(String[] args) {

        extracted();
        test1();
        test2();
        int sum = sum(1, 2, 3, 4, 5);
        System.out.println(sum);

    }

    private static void extracted() {
        TreeSet<Integer> set1 = new TreeSet<>();

        set1.add(10);
        set1.add(5);
        set1.add(15);
        set1.add(20);
        set1.add(1);
        System.out.println(set1);


        for (Integer i : set1) {
            System.out.println(i);
        }
    }

    public static void test1() {

        TreeSet<Student> set = new TreeSet<>();
        set.add(new Student("Tom", 20));
        set.add(new Student("Jerry", 18));
        set.add(new Student("Mary", 22));

        for (Student i : set) {
            System.out.println(i.getName() + " " + i.getAge());

        }
    }

    public static void test2() {
//        Comparator<Student> cmp = new Comparator<Student>() {
//            @Override
//            public int compare(Student o1, Student o2) {
//                return o1.getAge() - o2.getAge();
//            }
//        };
        Comparator<Student> cmp = (o1, o2) -> o1.getAge() - o2.getAge();
        TreeSet<Student> set = new TreeSet<>(cmp);
        set.add(new Student("Tom", 20));
        set.add(new Student("Jerry", 18));
        set.add(new Student("Mary", 22));
        for (Student i : set) {
            System.out.println(i.getName() + "----" + i.getAge());
        }
    }

    public static int sum(int ...a){

        int total = 0;
        for (int i : a){
            total += i;
        }
        return total;

    }
}
